define([
	'app/service/RoomService',
	'app/service/LectureService',
	'app/utils/UploadImgUtils',
	'app/service/QiniuService'
	],function (RoomService, LectureService, UploadImgUtils, QiniuService) {

	var opt = {};

	function img_beforeuploadFunc(up,file){
		$.processing("上传中...", 1);
	}

	function img_uploadedFunc(info){
		$.hidePreloader();
		
		var imgurl = $.wjtBaseCdn + info.key;

		

		QiniuService.getImageInfo(info.key, function(resp){

			
			var ratio = parseFloat($("#ratio").data("ratio"));
			
			var s_w = 1280;
			var s_h = parseInt(1280 * ratio);

			var img_w = parseInt(resp.width);
			var img_h = parseInt(resp.height);

			var _w = s_w;
			var _h = s_h;

			if(img_w * ratio <= img_h){

				if(img_w > s_w){
					_w = s_w;
				}else{
					_w = img_w;
				}

				_h = parseInt(_w * ratio);

			}

			var imgtools ="?imageView2/1/w/"+_w+"/h/"+_h+"/q/100/format/jpg/interlace/1";
			var final_imgUrl = imgurl + imgtools;

			$.confirm('是否使用此图片<img style="width:100%" src="'+final_imgUrl+'">', function(e){
				//ok

				$.processing("上传中...", 800, function(){

					// uploadLecture: function(roomuuid,pageurl,type,num,unionid,thumburl,callback_Func){
					var roomuuid = $("#roomUuid").val();
					var pageurl = final_imgUrl;
					var type = "img";
					var num = "20";
					var unionid = $.getHashUrlParam("unionid");
					var thumburl = null;
					LectureService.uploadLecture(roomuuid,pageurl,type,num,unionid,thumburl, function(resp){

						$.hidePreloader();
						if(resp.status=="200"){
							$.refreshPage();
						}else{
							$.toast("请检查您的网络连接是否正确");
						}

					});

				});

			});			
		});




	}

	function img_errorFunc(up, err, errTip){
		$.hidePreloader();
		if(err.code == -600){
			$.toast("上传的图片大小不能超过6MB");
		}else{
			$.toast("您的网络连接不太稳定，请重试");
		}
	}

	function selectImage(){
		
		
	}


	function selectVideo(){
		$.alert("功能还在完善中...");
	}

	function selectLive(){
		$.alert("功能还在完善中...");
	}

	function pageDeleteOps(e){

		var lectureid = $(this)[0].opt.lectureid;


		$.confirm('是否删除', function () {


			$.processing("正在删除...", 800, function(){

				LectureService.deleteLecturePage(lectureid, function(resp){

					$.hidePreloader();

					if(resp.status == "500"){
						$.toast("删除失败，请您重试");
					}else{
						$.refreshPage();
					}

				});

			});


        	
      	});

		
	}
	return {

		directive_orderButton: function(){
			var num_values = [];
			for(var i =1 ; i<  100; i++){
				num_values[i-1] = ""+i;
			}
			$(".orderButton .num").picker({
				onClose: function(){
					
					var lectureId = $($(this)[0].input).data("lectureid");
					var num_orginal = $($(this)[0].input).data("orginalnum");
					var num = $(this)[0].input.value;
					
					if(num == num_orginal){
						return;
					}

					$.processing("更新中...",1000,function(){
						
						LectureService.updateLectureOrder(lectureId, num, function(resp){

							console.log(resp);
							$.hidePreloader();
							if(resp.status=="200"){

								$.refreshPage();

							}else{

								$.toast("请检查您的网络连接是否正确");
							}
							
						});
					});

				},
				toolbarTemplate: '<header class="bar bar-nav">\
				<button class="button button-link pull-right close-picker">确定</button>\
				<h1 class="title">选择顺序</h1>\
				</header>',
				cols: [
				  {
				    textAlign: 'center',
				    values: num_values
				  }
				]
			});
			$(".orderButton").click(function(){
				$(this).find(".num").picker("open");
			});
			
		},
		directive_closeit: function(){
			
			$(".closeit").click(function(){

				var roomUuid = $("#roomUuid").val();
				$.confirm('直播关闭后将不能重新开启',
			        function () {
			          
			          	$.processing("关闭中...",1000, function(){

				        	RoomService.updateRoomStatus("3",roomUuid, function(resp){

				        		$.hidePreloader();

				        		if(resp.status == "200"){
				        			$.refreshPage();
				        		}else{
				        			$.toast("请检查您的网络连接是否正确");
				        		}
				        		
				        	});

			          	});

			        },
			        function () {
			          
			        }
		    	);


			});

		},

		directive_moreOperation: function(){

			$(".more-operation").click(function(){

				var opt = {};
				opt.lectureid = $(this).data("lectureid");

				
				var ops = [];
				ops[0] = $.genModalButton("删除", pageDeleteOps, opt);
				ops[1] = $.genModalButton("删除", pageDeleteOps, opt);

				$.promptMoreOperation(ops, opt);
				
				
			});

		},
		directive_pptMoreOperation: function(){


			$(".ppt-more-operation").click(function(){

				var opt = {};
				opt.lectureid = $(this).data("lectureid");

				if($(this).data("type") == "img"){

					var ops = [];
					ops.push($.genModalButton("删除", pageDeleteOps, opt));

					$.promptMoreOperation(ops, opt);
				}
				if($(this).data("type") == "video"){
					var ops = [];
					ops.push($.genModalButton("删除", pageDeleteOps, opt));
					
					$.promptMoreOperation(ops, opt);
				}
				
				if($(this).data("type") == "live"){
					var ops = [];
					ops.push($.genModalButton("删除", pageDeleteOps, opt));
					
					$.promptMoreOperation(ops, opt);
				}
				
			});

		},
        directive_addNewResource: function(){

        	

        	$(".create-actions").click(function(){


        		$.promptAddResource(function(){},function(){

        			var buttons1 = [
			        {
			          text: '请选择',
			          label: true
			        },
			        {
			        	text: '<div id="img_container"><span id="img_button" style="width:100%;height:100%;">添加图片</span></div>',
			          bold: true,
			          
			          onClick: selectImage
			        },
			        {
			          text: '添加视频',
			          bold: true,
			          
			          onClick: selectVideo
			        },
			        {
			          text: '添加直播',
			          bold: true,
			          
			          onClick: selectLive
			        }
			      ];

			      var buttons2 = [
			        {
			          text: '取消',
			          bg: 'danger'
			        }
			      ];
			      var groups = [buttons1, buttons2];
			      $.actions(groups);

			      UploadImgUtils.init("img_container","haojsqiniu",img_beforeuploadFunc,img_uploadedFunc, img_errorFunc);
			      
        		});


		  });

          
			
        }
	};

});
